package com.jrx.anytxn.transaction.job.collection.config;

import com.jrx.anytxn.transaction.config.FilePathsConfig;
import com.jrx.anytxn.transaction.entity.TlCollectionLoanList;
import com.jrx.anytxn.transaction.job.collection.step.CollectionFileListener;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlCollectionLoanListMapper;
import com.jrx.anytxn.transaction.service.ITlCollectionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;


/**
 * 生成催收文件
 *
 * @author lbz
 */
@Configuration
@EnableConfigurationProperties(FilePathsConfig.class)
public class CollectionFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(CollectionFileConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Resource
    private ExtTlCollectionLoanListMapper extTlCollectionLoanListMapper;
    @Autowired
    private ITlCollectionService tlCollectionService;
    @Autowired
    private FilePathsConfig filePathsConfig;
    @Autowired
    private CollectionFileListener collectionFileListener;

    @Bean
    public Job collectionFileJob() {
        return jobBuilderFactory.get("collectionFileJob").incrementer(new RunIdIncrementer()).listener(collectionFileListener)
                .start(collectionFileStep())
                .build();
    }

    @Bean
    public Step collectionFileStep() {
        return stepBuilderFactory.get("collectionFileStep")
                .tasklet(collectionFiletasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet collectionFiletasklet() {
        return (stepContribution, chunkContext) -> {
            logger.debug("催收文件开始生成。。。。。。");
            String filePath = filePathsConfig.getCollectionFilePath();
            String fileName = filePathsConfig.getFileName();
            Integer collectionPoolSize = filePathsConfig.getCollectionPoolSize();
            List<TlCollectionLoanList> tlCollectionLoanLists = extTlCollectionLoanListMapper.selectAll();
            tlCollectionService.createFile(tlCollectionLoanLists,filePath,fileName,collectionPoolSize);
            logger.debug("催收文件处理完成");
            return RepeatStatus.FINISHED;
        };
    }

}
